import socket
from PyQt5.QtWidgets import *
from threading import Thread

class WordReceiver:
    def __init__(self,parent,addr,port):
        print("a new word receiver")
        self.parent = parent
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.addr = addr
        self.port = port
        self.s.bind((addr,port))
        self.s.listen(255)
        while True:
            client,_ = self.s.accept()
            print(client)
            newReceiveThread = Thread(target=lambda:self.receiveWord(client,self.parent))
            newReceiveThread.start()
            

    def receiveWord(self,client,parent):
        while True:
            try:
                data = client.recv(1024)
                print(data.decode('utf-8'))
            except Exception:
                client.close()
                print("由于断开，关闭了一个文字接收线程")
                break
            parent.newMessageSignal.emit(data.decode('utf-8'))


if __name__ == '__main__':
    myWordReceiver = WordReceiver("192.168.0.4",913)
    myWordReceiver.receiveWord()